﻿///#undef MySql
///#define MSSql
#define MySql

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

using MySql.Data.MySqlClient;

namespace ukeasbjWeb
{
    public partial class Employees : System.Web.UI.Page
    {
        //从配置文件Web.Config 中读出数据库设置
        //string strCnn = ConfigurationManager.AppSettings["glCnnStr"];
        ///string MySqlstrCnn = ConfigurationManager.AppSettings["MySqlCnnStr"];
        
        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            Common_SXD.Is_LongIn(Response, Session, Request);

            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            bool bIs_G_M = Convert.ToBoolean(Session["Is_G_M"]);
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_HR = Convert.ToBoolean(Session["Is_HR"]);
            bool bIs_Marketing = Convert.ToBoolean(Session["Is_Marketing"]);

            if (!bIs_O_M && !bIs_Admin && !bIs_Accountant && !bIs_HR)
            {
                Response.Write("<h2 align='center'><font face='宋体' color='red'>");
                Response.Write("<br>对不起，您不能使用本功能。" +
                    "<br>请用其它身份登录。");
                Response.Write("</fong></h2>");
                Response.End();

            }

            if (!IsPostBack)
            {
                Common_SXD.Init_Office(DDList_Office);
                Init_Table_Employees();

            }

        }

        private void Init_Table_Employees()
        {
            //  在此处放置用户代码以初始化页面
            //////////////////////////////////////////////////////
            string sBClassID = DDList_Office.SelectedItem.Text;
            string sSearchValue = TextBox_SearchValue.Text.Trim();
            bool bIs_G_M = Convert.ToBoolean(Session["Is_G_M"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_HR = Convert.ToBoolean(Session["Is_HR"]);
            bool bIs_Marketing = Convert.ToBoolean(Session["Is_Marketing"]);


            string sSql = "select Email, C_N_Employee, E_N_Employee, Code_Employee, Office, Suffix_No_Contract, Is_Active, Is_MOTP, Is_Counsellor, Is_Writer, Is_Admin, Is_G_M, Is_O_M, Is_Accountant, Is_Cashier, Is_Collector, Is_Receptionist, Is_Frontline_Agent, Is_HR, Is_Marketing  from Employees where";
            if (!bIs_Admin && !bIs_HR )
            {
                Common_SXD.SetSelectedIndex(Session["Office"].ToString().Trim(), DDList_Office);
                DDList_Office.Enabled = false;
                sSql += " Office = '" + Session["Office"] + "' and ";
            }
            else
            {
                DDList_Office.Enabled = true;
                if (sBClassID != "总部")
                {
                    sSql += " Office = '" + sBClassID + "' and ";
                }
                else
                {
                    sSql += " ";
                }
            }
            if (DDList_Active.SelectedItem.Text == "活跃")
            {
                sSql += " Is_Active = 1 and ";
            }
            else if (DDList_Active.SelectedItem.Text == "离职(活跃)")
            {
                sSql += " Is_Active = 1 and ";
                /// 离职时间有效
                /// 
                sSql += " Date_Departure > '1930-1-1' and ";
            }
            else if (DDList_Active.SelectedItem.Text == "休眠")
            {
                sSql += " Is_Active = 0 and ";
            }
            
            /// 滤除姓名、邮件等等
            sSql += "(  Code_Employee  like '%" + sSearchValue + "%' ";
            sSql += "or  C_N_Employee  like '%" + sSearchValue + "%' ";
            sSql += "or  E_N_Employee  like '%" + sSearchValue + "%' ";
            sSql += "or  Email  like '%" + sSearchValue + "%' ";
            sSql += "or  Suffix_No_Contract  like '%" + sSearchValue + "%' )";

            sSql += " order by Office, Code_Employee";

#if MySql
            MySqlConnection MySqlconn = new MySqlConnection();
            MySqlDataReader MysqlReader = Common_SXD.Open_MySqlDataReader(sSql, MySqlconn, "打开数据库出错：", Response);
            
            // 读入第一个查询结果集， 即 Contract 中的数据
            int nCol = MysqlReader.FieldCount;
#elif MSSql
            ///打开数据库，获取sqlReader变量
            SqlConnection sqlCnn = new SqlConnection();
            SqlDataReader sqlReader = Common_SXD.Open_MSSqlDataReader(sSql, sqlCnn, "打开数据库出错：", Response);
            // 读入第一个查询结果集， 即 Contract 中的数据
            int nCol = sqlReader.FieldCount;
#endif

            //清除表中的内容
            Table_Employees.Rows.Clear();

            //首先生成标题行
            {
                HtmlTableRow row = new HtmlTableRow();
                for (int i = 0; i < nCol; i++)
                {
                    // Create a new cell and add it to the Cells collection.
                    HtmlTableCell cell = new HtmlTableCell();
#if MySql
                    string temp = Common_SXD.English_Chinese(MysqlReader.GetName(i));
#elif MSSql
                    string temp = Common_SXD.English_Chinese(sqlReader.GetName(i));
#endif
                    cell.Controls.Add(new LiteralControl(temp.Trim()));
                    row.Cells.Add(cell);
                }
                {
                    HtmlTableCell cell = new HtmlTableCell();
                    cell.Controls.Add(new LiteralControl("修改"));
                    row.Cells.Add(cell);
                }
                Table_Employees.Rows.Add(row);
                row.BgColor = "Tan";
            }

            // 依次读出数据
            int j = 0;
#if MySql
            while (MysqlReader.Read())
#elif MSSql
            while (sqlReader.Read())
#endif
            {
                HtmlTableRow row = new HtmlTableRow();
                //				String sNo_Series = sqlReader.GetString(0); 
#if MySql
                String sEmail = MysqlReader.GetValue(0).ToString();
#elif MSSql
                String sEmail = sqlReader.GetValue(0).ToString();
#endif
                // Provide a different background color for alternating rows.
                if (j % 2 == 1)
                    row.BgColor = "Gainsboro";

                // Iterate through the cells of a row.
                //			{
                //				HtmlTableCell cell = new HtmlTableCell();
                //				cell.Controls.Add(new LiteralControl("<a href=\"JavaScript:comOpen('./EmployeeEdit.aspx?Series_No=ReadOnly:" + 
                //					sEmail.Trim() + 
                //					"',800,420);\">" +
                //					sEmail.Trim() +
                //					"</a>"));
                //				row.Cells.Add(cell);
                //			}

                for (int i = 0; i < nCol; i++)
                {
                    // Create a new cell and add it to the Cells collection.
                    HtmlTableCell cell = new HtmlTableCell();
#if MySql
                    cell.Controls.Add(new LiteralControl(Common_SXD.TimeToYMD(MysqlReader.GetValue(i).ToString())));
#elif MSSql
                    cell.Controls.Add(new LiteralControl(Common_SXD.TimeToYMD(sqlReader.GetValue(i).ToString())));
#endif

                    row.Cells.Add(cell);
                }
                {
                    HtmlTableCell cell = new HtmlTableCell();
                    if (bIs_O_M || bIs_Admin || bIs_HR)
                    {//////地区经理能修改数据
                        ///系统管理员修改数据
                        ///人力资源修改数据
                        cell.Controls.Add(new LiteralControl("<a href=\"JavaScript:comOpen('./EmployeeEdit.aspx?Email=" +
                            sEmail.Trim() +
                            "',900,420);\">详细数据/修改</a>"));
                        row.Cells.Add(cell);
                    }
                }

                Table_Employees.Rows.Add(row);
                j++;
            }
#if MySql
            MysqlReader.Close(); // 关闭sqDataReader
            MySqlconn.Close(); // 关闭数据库连接
            
#elif MSSql
            sqlReader.Close(); // 关闭sqDataReader
            sqlCnn.Close(); // 关闭数据库连接
#endif
        }

        protected void Bt_Search_Click(object sender, EventArgs e)
        {
            Init_Table_Employees();
        }
    }
}
